var StudentHoods = function() {
	
	var ajaxSize = false;
	
	function init() {
		$(document.body).removeClass('no-js').addClass('js');
		
		attachOrderInfo();
		attachColourOnChange();
		attachPlaceholder();
		attachSlideShow();
		attachFancybox();
        attachFormFocus();
		pngFix();
	}
	
	function attachFancybox(){
		if ($('a.fancybox').length > 0) {
			$('a.fancybox').fancybox({
				aspectRatio: true,
				autoSize: false,
				closeClick: false,
				closeEffect: 'fade',
				closeSpeed: 'normal',
				fitToView: false,
				height: '70%',
				maxWidth: 1200,
				maxHeight: 800,
				openEffect: 'fade',
				openSpeed: 'normal',
				width: '70%'
			});
		}
	}
	
    function attachFormFocus(){
        if ($('.error-summary').length >0 ){
            $('.error-summary a').click(function(){
                var tar = $(this).attr('href');
                $(tar).focus();
                return false;
            });
        }
    }

	function attachPlaceholder(){
		if ($('input').length < 1 || $('textarea').length < 1) {
			return false;
		};
		Placeholder.init({
		    normal         : "#666666",
		    placeholder : "#555555"
		});
	}

	function attachSlideShow()
	{
		if ($('#slider').length > 0) 
		{
			$('.slideshow').cycle({
			fx:     'fade',
			speed:   1000,
			timeout: 8000,
			pause:   1,
			pager: '#slider-nav'});
		}

		if ($('.list-testimonials').length > 0) 
		{
		   $('.list-testimonials').cycle({
			   fx:     'fade',
			   speed:   1000,
			   timeout: 8000,
			   pause:   1,
			   pager: '.testimonials-nav'});
		}
	}

	function attachColourOnChange()
	{
        if ($('#id_colour_select').length > 0)
        {
        	if ($('.wrap_id_size').length > 0)
        	{
        		$('.wrap_id_size').css({display: 'none'});

        		$('#id_colour_select').find('input[type="radio"]').change(function()
        		{
                    var colour = $(this).val();
                    loading($('.wrap_id_size'), colour);
        			$.ajax({url: convertToSlug(colour)+'/',
        					context: document.body,
			        		success: function(msg)
			        		{
			        			$(".wrap_id_size").html(msg);
			        			
			        			if ($('.header-hoodie').length > 0)
			        			{	
			        				$('.header-hoodie').attr('style','background: url(' + colourData[colour] + ') no-repeat 0 0');
			        			}
			        			
			        			
			        		}});
			        if (!$('.wrap_id_size').is('visible')) {
                        $('.wrap_id_size').css({display: 'block'});
                        $('.wrap_id_size').addClass('ajaxed');
                    }
        		});	
        	}
        }
	}

    function loading($cont, colour){
        if ($cont.hasClass('ajaxed')) {
            $cont.find('label').text('Loading in sizes for '+colour);
            $cont.find('select').attr('disabled', 'disabled');
        }
    }


	function ajaxSuccess()
	{
		$('.wrap_id_size').remove();
	}

	function attachOrderInfo(){
		if ($('.list-trigger').length > 0) {
			$('.list-trigger dt').each(function(){
				var text = $(this).text();
				$(this).html('<a href="#">'+text+'</a>');
			});
		
			$('.list-trigger dt a').click(function(){	
				
				var $parent = $(this).parent();
				
				if ($parent.hasClass('open')) {
					slideUp($parent);
					return false;
				};
				
				$('.list-trigger').find('dt').each(function(){
					if ($(this).hasClass('open')) {
						slideUp($(this));
					}
				});
				
				$parent.next().slideDown(function(){
					$parent.addClass('open');
				});
				
				return false;
			});
			
		}
	}
	
	function slideUp($this){
		$this.next().slideUp(function(){
			$this.removeClass('open');
		});
	}
	
	function pngFix() {
		if (typeof(jQuery.fn.pngFix) == 'function') {
			$(document).pngFix();
		}	
	}
	
	function convertToSlug(value)
	{
	    return value.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-');
	}


	return {
		init: init,
		ajaxSize: ajaxSize
	}
}();

$(document).ready(function(){
	StudentHoods.init();
});